<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>5 minutes tutorial sample</title>

		
        <script type="text/javascript" src="http://ndjin.googlecode.com/svn/trunk/js/lib/model.js"></script>
        <script type="text/javascript" src="http://ndjin.googlecode.com/svn/trunk/js/lib/instanceHelper.js"></script>
    </head>
	
    <body>
        <script>
        	var applicationDomainName = "your-application-domain-name";
			
			// Model Part : we create a Contact object
            var model = new Model( applicationDomainName, false );
            model.init();
			
			var dataType = {
				name: "Contact",
				inheritedDataTypeId: model.getDataTypeIdByName( 'Object' ),
				fields: [
					{ name: 'name', dataTypeId: model.getDataTypeIdByName( 'Text' ), min: 0, max: 1, unique: false, preview: true },
					{ name: 'telephone', dataTypeId: model.getDataTypeIdByName( 'Text' ), min: 0, max: 1, unique: false, preview: false },
				]
			}
			model.createOrUpdateDataType( dataType );
			
			// Model Part : we create a contact collection and set it as Application field
			var applicationDataType = model.getDataTypeByName( 'Application' );
			applicationDataType.fields.push( { name: 'contacts', dataTypeId: model.getDataTypeIdByName( 'Contact' ), min: 0, max: 100, unique: false, preview: false } );
			model.createOrUpdateDataType( applicationDataType );			




			
			
			var instanceHelper = new InstanceHelper( applicationDomainName, false );
			
			// Instance Part - we create an instance of Contact and append it in Application 'contacts' collection 
			var query = 
			{ 
				instance: 
				{
					name: 'MySelf',
					telephone: '0123456789',
					_applyTransitions: [ 
						{ name:'New',   parameters: { ownerFieldName: 'contacts' } },
						{ name:'Store', parameters: { ownerFieldName: 'contacts' } }
					]
				}
			};
			var contact = instanceHelper.applyTransition( query );
			
			
			
			// Instance Part - we update our instance of Contact
			query = 
			{ 
				instance: 
				{
					_id: contact._id,
					name: 'Noboby',
					_applyTransitions: [ { name:'Update' } ]
				}
			};
			contact = instanceHelper.applyTransition( query );
			
			
			
			// Instance Part - we query 10 Contacts instance from the Application 'contacts' collection
			query = 
			{
				ownerFieldName: "contacts",
				count: 10,
			};
			var instances = instanceHelper.getInstances( query );
			

			// Instance Part - we delete those 10 instances from 'contacts' collection
			for( var i=0; i<instances.length; i++ )
			{
				var instance = instances[i];
				var query = 
				{ 
					instance: {
						_id: instance._id,
						_applyTransitions: [{ name: 'Delete'} ]
					}
				};
				instanceHelper.applyTransition( query );
			}

        </script>
        5 minutes tutotial
    </body>
</html>
